import axios from "axios";
import Redis from "ioredis";
import { WeeksMap, UserAgent, Weeks } from "../content";

export const redis = new Redis({
  family: 4,
  host: "127.0.0.1",
  password: "",
  port: 6379,
  db: 1,
});

const got = axios.create({
  headers: {
    "user-agent": UserAgent,
  },
});

export async function scrawl(cid: string, pid: string = "latest") {
  // @ts-ignore
  const mapData = WeeksMap[cid];
  if (!mapData) {
    return {
      cid,
      pid,
      content: "",
      title: "",
      isNew: false
    };
  }
  let row = (await redis.get(`${cid}-${pid}`)) || "";
  let title = "";
  let content = "";
  if (row) {
    try {
      const data = JSON.parse(row);
      return {
        cid,
        pid,
        content: data.content,
        title: data.title,
        isNew: false
      };
    } catch (err) {}
  }
  let url = mapData.uri + "/issues/" + pid;
  if (cid === "21") {
    // python
    if (pid === "latest") {
      url = mapData.uri + "/latest";
    }
    const { data } = await got(url);
    const $title = data.match(/<title>([^<]+)<\/title>/)[1];
    pid = $title.split("#")[1];
    title = data.match(/"description" content="([^.]+)/)[1];
    // const date = $title.split("published ")[1];
    content = "<div"  + data.split('"mcnTextContent"')[3].split("</td>")[0] + "</div>";
    return {
      cid,
      pid,
      content,
      title,
      isNew: true
    };
  }
  if (cid === "22") {
    // Vue
    if (pid === "latest") {
      url = mapData.uri;
    }
    const { data } = await got(url);
    title = data.match(/issue-title">([^<]+)/)[1];
    pid = data.match(/issues\/([\d]+)/)[1];
    content = data.split('Go back</a>')[1].split('</article>')[0];
    // const date = content.match(/issue-date">([^<]+)/)[1];
    return {
      cid,
      pid,
      title,
      content,
      isNew: true,
      stylesheet: 'vue.css'
    };
  }
  if (String(cid) === "23") {
    // CSS
    url = `${mapData.uri}/issue-${pid}/`;
    const { data } = await got(url);
    let $title = data.split("</head>")[0];
    console.log($title)
    pid = $title.match(/Issue #([\d]+)/)[1];
    // const date = $title.match(/pubdate>([^<]+)/)[1];
    title = `Issue #${pid}`;
    content = '<section><header class="newsletter-head"' + data.split('class="newsletter-head"')[1];
    content = content.split("<aside")[0] + '</section>';
    return {
      cid,
      pid,
      title,
      content,
      isNew: true,
      stylesheet: 'css.css'
    };
  }
  if (cid === "24") {
    // cron
    url = `${mapData.uri}/issue-${pid}/`;
    const { data } = await got(url);
    // const date = data.match(/article:published_time" content="([^"]+)/)[1];
    const titles = data.match(/cron.weekly issue #([\d]+): ([^<]+)/);
    title = titles[2];
    pid = titles[1];
    content = data.split("content blog-content cronweekly-content")[1];
    content = content.split('<aside class="hidden')[0];
    content = '<div class="cronweekly-content' + content;
    content = content.split("<h1>Jobs")[0] + "</div>";
    return {
      cid,
      pid,
      title,
      content,
      isNew: true,
      stylesheet: 'cron.css'
    };
  }
  if (cid === "25") {
    // web tools issue
    url = `${mapData.uri}/archives/issue-${pid}/`;
    const { data } = await got(url);
    title = data.match(/<title>[^\(]+\(([^\)]+)\)<\/title>/)[1];
    content = '<div style="max-width:600px;margin:0px auto 0px auto;' + data.split('<div style="max-width:600px;margin:0px auto 0px auto;')[1].split('<!-- <script defer src="https://cdn.commento.io/js/commento.js')[0]
    return {
      cid,
      pid,
      title,
      content,
      isNew: true,
    };
  }
  if (cid === "26") {
    // TODO: 无法根据id来匹配链接
    const { data } = await got(url);
    pid = data.match(/This Week in Rust ([\d]+)<\/a>/)[1];
    // const date = body.match(/<time[^>]+>([^\<]+)</)[1];
    content = data.split('<article class="post-content">')[1];
    content = content.split("</article>")[0];
    return {
      cid,
      pid,
      title,
      content,
      isNew: true,
    };
  }
  if (cid === "27") {
    // TODO: 相对路径的图片链接匹配
    const { data } = await got(`${mapData.uri}/news/${pid}`)
    const titles = data.match(/This Month in Rust GameDev #([\d]+) - ([^<]+)/);
    title = titles[2];
    pid = titles[1];
    content = '<main class="page-content"' + data.split('<main class="page-content"')[1].split('</main>') + '</main>'
    return {
      cid,
      pid,
      title,
      content,
      isNew: true,
    };
  }
  const { data } = await got(url);
  // console.log("data", data);
  title = data.match(/<title>([^<]+)<\/title>/)[1];
  content = data.split('<div id="content">')[1];
  content = content.split(
    `<!--[if (gte mso 9)|(IE)]></td></tr></table><![endif]-->`
  )[0];
  content = content
    .replace(/(\n)/g, "")
    .replace("</div>  </td></tr></table>", "");

  const date = title.split(": ")[1];
  pid = title.match(/( [\d]+):/)[1].trim();
  // console.log(content);
  redis.set(
    `${cid}-${pid}`,
    JSON.stringify({ title, content }),
    "EX",
    60 * 60 * 24 * 30
  );
  return {
    cid,
    pid,
    title,
    content,
      isNew: true,
  };
}
